def pipe(vlabel){
    def icos= "\u26F0" //montain
    def icot= "\u261B" //pointRightBlack +

	node(vlabel) {ansiColor('xterm') {
		//TODO: CHECK commitID

		// TODO: 
		// 1.tpl_id: pack成一个压缩包 |lastCommitID: 确认打包脚本被执行;
		// 2.find xx/tpl_id > 清单文件
		// 
		stage(icos+'TPL'){ container('tpl'){ 
			task(icot+'template')
			echo "tpl"
			def tplId='01java_springboot'
			def module="t1-module"

			def templater = libraryResource 'libs/templater.sh'
			writeFile file: './templater.sh', text: templater

			def tplPath1 = libraryResource "templates/layouts/${tplId}.tgz"
			writeFile file: "${tplId}.tgz", text: tplPath1

			// sh "pwd && ls -lh"
			sh """
				set +x
				#clone
				apk add git #git here?
				auth=rop2:4b3de1d72e77878dec7b095e0a3c3bf5
				layoutRepo=https://\$auth@gitee.com/infrastlabs/layouts.git
				rm -rf layouts && git clone \$layoutRepo layouts

				#unpack
				dest=tpl && rm -rf \$dest && mkdir -p \$dest
				tar -xf ${tplId}.tgz -C \$dest #tar -zxf: tar -z not suported with busybox.
				ls \$dest/

				#templater
				dest=layouts/$module && rm -rf \$dest && mkdir -p \$dest
				#cat dcp.yml > \$dest

				export GENERATE="gen@\$(date '+%Y-%m-%d %H:%M:%S')"
				export IMG_NAME=$module
				export APP_NAME=$module
				sh templater.sh tpl/dcp.yml > \$dest/dcp.yml
				sh templater.sh tpl/k8s.yml > \$dest/k8s.yml
				sh templater.sh tpl/sv.conf > \$dest/sv.conf

				#exit 0
				#commit_push
				git config --global user.name "rop2"
				git config --global user.email "rop2-jk-auto@qq.com"
				cd layouts
				  git add -A .
				  git commit -m "auto@\$(date '+%Y-%m-%d %H:%M:%S')"
				  git push origin master
			"""
		}}
	}}
}


def deploy(id) {
    def vlabel = "k3s-${UUID.randomUUID().toString().substring(0,4)}"
    def utils = new com.gitee.infrastlabs.Utils()
    ret = utils.getEmpSlave("k3s-")
    if ( ret.length() != 0) {
		vlabel = ret
    }
    //, readOnly: true
    podTemplate(label: vlabel, cloud: 'kubernetes', 
			containers: [
			  containerTemplate(name: 'jnlp', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-jnlp', alwaysPullImage: true, args: '${computer.jnlpmac} ${computer.name}'),
			  containerTemplate(name: 'git', image: 'alpine/git', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
			  containerTemplate(name: 'tpl', image: 'registry.cn-shenzhen.aliyuncs.com/infrastlabs/alpine-ext:weak', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
			  containerTemplate(name: 'maven', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-maven', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
              containerTemplate(name: 'docker', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-docker', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
              containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.10.2', command: 'cat', ttyEnabled: true)
			],
          volumes: [
				hostPathVolume(hostPath: '/opt/jenkins/shell', mountPath: '/opt/shell'),
				hostPathVolume(hostPath: '/opt/jenkins/jk_ci_data', mountPath: '/opt/jk_ci_data')
            ],
          imagePullSecrets: [ 'regsecret' ],
          idleMinutes: 600,
          annotations: [
				podAnnotation(key: "sidecar.istio.io/inject", value: "false")
           ]
    ) { this.pipe(vlabel) }  
}

def call() {
    deploy('aa')
}